//
//  TTCompanyDescribeViewController.m
//  TaoTuo
//
//  Created by admin on 2019/11/9.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTCompanyDescribeViewController.h"

@interface TTCompanyDescribeViewController ()<YYTextViewDelegate>

@property (nonatomic, strong) YYTextView *textView;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, copy) NSString *content;  // 输入的总内容

@property (nonatomic, copy) FinishedBlock finishBlock;

@end

@implementation TTCompanyDescribeViewController

- (instancetype)initWithContent:(NSString *)content finishBlock:(FinishedBlock)finishBlock
{
    self = [super init];
    if (self) {
        self.content = content ? : @"";
        self.finishBlock = finishBlock;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"公司简介";
    
    UIButton *saveBtn = [UIButton createButtonWithTitle:@"完成" titleColor:kMainColor titleFont:[UIFont systemFontOfSize:15] backgroundColor:nil target:self action:@selector(completeAction)];
    saveBtn.frame = CGRectMake(0, 0, 40, 44);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
    
    [self setupSubViews];
    
}

// 点击完成
- (void)completeAction
{
    if (self.finishBlock) {
        self.finishBlock(self.textView.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)backAction
//{
//    LLSystemAlertView *alertView = [LLSystemAlertView alertViewWithTitle:@"提示" message:@"如不保存，填写的信息将会丢失" cancelTitle:@"取消" cancelBlock:^{
//        [self.navigationController popViewControllerAnimated:YES];
//    } confirmTitle:@"保存" confirmBlock:^BOOL{
//        [self completeAction];
//        return YES;
//    }];
//    [TYShowAlertView showAlertViewWithView:alertView backgoundTapDismissEnable:YES];
//}

- (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (text.length == 0) {
        return YES;
    } else {
        return _content.length <= 150;
    }
}


- (void)setupSubViews
{
    _textView = [[YYTextView alloc] initWithFrame:CGRectMake(15, 15, self.view.width - 30, 182)];
    _textView.delegate = self;
    _textView.backgroundColor = UIColorHex(F1F5F6);
    _textView.placeholderText = @"请输入具体内容";
    _textView.placeholderTextColor = UIColorHex(DEDEDE);
    _textView.placeholderFont = [UIFont systemFontOfSize:13];
    _textView.font = [UIFont systemFontOfSize:15];
    _textView.textColor = UIColorHex(333333);
    _textView.layer.cornerRadius = 5;
    _textView.text = self.content;
    [self.view addSubview:_textView];
    
    NSString *str = [NSString stringWithFormat:@"%ld/150", _content.length];
    _countLabel = [UILabel labelWithTitle:str color:UIColorHex(999999) font:[UIFont systemFontOfSize:13] alignment:NSTextAlignmentRight];
    _countLabel.frame = CGRectMake(15, _textView.bottom - 20, _textView.width, 20);
    [self.view addSubview:_countLabel];
    
    [_textView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    _content = change[NSKeyValueChangeNewKey];
    _countLabel.text = [NSString stringWithFormat:@"%ld/150", _content.length];
}


- (void)dealloc
{
    [_textView removeObserver:self forKeyPath:@"text"];
    NSLog(@"[< %@ >] --> 释放了", [self className]);
}

@end
